package com.zyguo.sdk.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class BUtils {
	private static String dateFormatStr = "yyyy-MM-dd HH:mm:ss";
	private static String longFormatStr = "yyyyMMddHHmmss";
	private static String dayFormatStr  = "yyyy-MM-dd";
	
	public static boolean isBlank( String s ){
		if( s != null && s.trim().length() >  0 ){
			return false;
		}
		return true;
	}
	
	public static boolean isNotBlank( String s){
		return !isBlank( s ); 
	}  
	
	//返回 2014-07-08 00:00:00格式
	public String date2str( Date d ){
		SimpleDateFormat sf = new SimpleDateFormat( dateFormatStr );
		return sf.format( d );
	}
	
	//2014-07-08 00:00:00 转date
	public static Date str2date( String str ){
		if( isNotBlank( str ) ){
			try {
				SimpleDateFormat sf = new SimpleDateFormat( dateFormatStr );
				return sf.parse( str );
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	//返回 20140708010101格式 , 不是时间戳
	public static String date2longTime( Date d ){
		if( d != null ){
			SimpleDateFormat sf = new SimpleDateFormat( longFormatStr );
			return sf.format( d );
		}
		return null;
	}
	
	//
	public static Date longTime2date( String str ){
		if( isNotBlank( str ) ){
			try {
				SimpleDateFormat sf = new SimpleDateFormat( longFormatStr );
				return sf.parse( str );
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	//返回 2014-07-08 
	public static String date2dayStr( Date d ){
		if( d != null ){
			SimpleDateFormat sf = new SimpleDateFormat( dayFormatStr );
			return sf.format( d );
		}
		return null;
	}
	
	//2014-07-08 转date
	public static Date dayStr2date( String str ){
		if( isNotBlank( str ) ){
			try {
				SimpleDateFormat sf = new SimpleDateFormat( dayFormatStr );
				return sf.parse( str );
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	//返回前一天日期
	public static Date getPrevDate(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, -1);// 把日期往后增加一天.整数往后推,负数往前移动
		return calendar.getTime();
	}

	//返回前几天日期
	public static Date getPrevDate(Date date, int i ) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, -i);// 把日期往后增加一天.整数往后推,负数往前移动
		return calendar.getTime();
	}
	
	//返回后一天日期
	public static Date getNextDate(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, 1);// 把日期往后增加一天.整数往后推,负数往前移动
		return calendar.getTime();
	}

	//返回后几天的日期
	public static Date getNextDate(Date date, int i ) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动
		return calendar.getTime();
	}	
	
	//是否是数字
	public boolean isNumber( String s ){
		if( isNotBlank( s ) ){
			return s.matches("[0-9]*");
		}
		return false;
	}
	
}
